package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.base.Page;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseTrademarkService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BaseTrademarkServiceImpl implements BaseTrademarkService {
    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;


    @Override
    public Page<BaseTrademark> getBaseTrademarkByPage(Long pageNum, Long pageSize) {
        int offset = (pageNum.intValue() - 1) * pageSize.intValue();
        int limit = pageSize.intValue();

        List<BaseTrademark> items = baseTrademarkMapper.selectByPage(offset, limit);
        int total = baseTrademarkMapper.countAll();

        Page<BaseTrademark> page = new Page<>();
        page.setRecords(items);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page.setTotal((long) total);
        return page;
    }

    @Override
    public void saveBaseTrademark(BaseTrademark baseTrademark) {
        Long id = baseTrademark.getId();
        //没有id说明是新增
        if(null==id){
            baseTrademarkMapper.insert(baseTrademark);
        }
    }

    @Override
    public void deletBaseTrademark(Long id) {
        QueryWrapper<BaseTrademark> baseTrademarkQueryWrapper = new QueryWrapper<>();
        baseTrademarkQueryWrapper.eq("id", id);
        baseTrademarkMapper.delete(baseTrademarkQueryWrapper);
    }

    @Override
    public BaseTrademark getBaseTrademark(Long id) {
        QueryWrapper<BaseTrademark> baseTrademarkQueryWrapper = new QueryWrapper<>();
        baseTrademarkQueryWrapper.eq("id", id);
        return baseTrademarkMapper.selectOne(baseTrademarkQueryWrapper);

    }

    @Override
    public void updateBaseTrademark(BaseTrademark baseTrademark) {
        baseTrademarkMapper.updateById(baseTrademark);
    }
}
